package com.longge;

import java.util.*;

public class MostCommonWord {

    public String mostCommonWord(String paragraph, String[] banned) {

        String[] strs = paragraph.replace("!", " ").replace("?", " ")
                .replace("'", " ").replace(",", " ")
                .replace(";", " ").replace(".", " ").toLowerCase().split(" ");

        ArrayList<String> words = new ArrayList<>();
        for (String str : strs) {
            String[] word = str.split(" ");
            words.addAll(Arrays.asList(word));
        }
        HashSet<String> set = new HashSet<>(Arrays.asList(banned));
        HashMap<String, Integer> map = new HashMap<>();
        for (String word : words) {
            if (!set.contains(word) && !"".equals(word)) {
                map.put(word, map.getOrDefault(word, 0) + 1);
            }
        }
        int max = 0;
        String str = "";
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() > max) {
                max = entry.getValue();
                str = entry.getKey();
            }
        }
        return str;

    }

    public static void main(String[] args) {

        String s = new MostCommonWord().mostCommonWord("Bob. hIt, baLl", new String[]{"bob", "hit"});
        System.out.println(s);
    }

}
